import { loadModules } from 'esri-loader';
import MapTools from './mapTools';
import queryTask from './queryTask';
import request from './request';
import config from './config';
import EsriJSONToGeoJSON from './EsriJSONToGeoJSON'
import echartsUtils from './echartsUtils';


let mapTools;

const analyze = {
    mapServer: {
        sanjiu: 'http://59.175.191.83:6080/arcgis/rest/services/SJ_QS_SJGZDK/MapServer',
        dikuaiUrl:"http://59.175.191.84:6080/arcgis/rest/services/GIS_YDXZ2016_DT/MapServer/0",
        xiaoxueUrl:"http://192.168.0.179:6080/arcgis/rest/services/ZXGK_XZ_ZXX/FeatureServer/0",
        xiaoxueId:"OBJECTID",
        xiaoxueDis:500,
        xiaoxueMianji:"用地面积HA",
        zhongxueUrl:"http://192.168.0.179:6080/arcgis/rest/services/ZXGK_XZ_ZXX/FeatureServer/0",
        zhongxueId:"OBJECTID",
        zhongxueDis:1000,
        zhongxueMianji:"用地面积HA",
        yiliaoUrl:"http://59.175.191.82:6080/ArcGIS/rest/services/ZXGK_XZ_YiLiao_P/MapServer/0",
        yiliaoShequUrl:"http://59.175.191.82:6080/ArcGIS/rest/services/ZXGK_XZ_YiLiao_P/MapServer/0",
        yiliaoShequId:"OBJECTID",
        yiliaoShequDis:500,
        yiliaoShequMianji:"建筑面积m2",
        yiliaoShijiUrl:"http://59.175.191.82:6080/ArcGIS/rest/services/ZXGK_XZ_YiLiao_P/FeatureServer/0",
        yiliaoShijiId:"OBJECTID",
        yiliaoShijiDis:5000,
        yiliaoShijiMianji:"建筑面积m2",
        yanglaoPointUrl:"http://www.wpdi.cn:8060/ArcGIS/rest/services/ZXKG_XZ_YLSS/MapServer/0",
        yanglaoShequId:"FID",
        yanglaoShequDis:500,
        yanglaoShequMianji:"建筑面积M2",
        yanglaoPolygonUrl:"http://www.wpdi.cn:8060/ArcGIS/rest/services/ZXKG_XZ_YLSS/MapServer/3",
        yanglaoShijiId:"FID",
        yanglaoShijiDis:1500,
        yanglaoShijiMianji:"建筑面积M2",
        lvdiUrl:"http://59.175.191.82:6080/arcgis/rest/services/SJ_ZXGK_XZ_GYLD/MapServer/0",
        lvdiId:"FID",
        lvdiDis:500,
        lvdiMianji:"area",
    },
    nowBiaozhun:{
		xiaoxue:7.4,
		chuzhong:9.33,
		yiliao:0.9,
		yanglao:0.2,
		lvdi:8,
		tiyu:0.6,
		tingchechang:0.5,
		gongjiao:0.324
	},
    pgresult: function(map, geometry, dikuaiObjs, sheshi, biaozhun, mianji, bufferDistance, resultLayer){
        // 地块要素集
        let dikuaiGeos = [];
        for(let i=0;i<dikuaiObjs.length;i++){
            dikuaiGeos.push(dikuaiObjs[i].g.geometry);
        }
        //循环设施，计算每个地块的指标值
        for(let i=0;i<sheshi.length;i++){
            let sheshiBuffer = mapTools.bufferGeo(sheshi[i].geometry,bufferDistance,9001);
            if(!sheshiBuffer){
                continue
            }
            let intersect = mapTools.intersect(dikuaiGeos,sheshiBuffer);

            let renkou = 0;
            for(let j=0;j<intersect.length;j++){
                if(intersect[j]){
                    renkou += dikuaiObjs[j].renkou;
                }
            }

            let zhibiao = renkou && sheshi[i].attributes[mianji]/renkou || 0;
            // 
            for(let j=0;j<intersect.length;j++){
                if(intersect[j]){
                    dikuaiObjs[j].zhibiao += zhibiao;
                }
            }
        }
        //结果显示
        if(resultLayer instanceof esri.layers.GraphicsLayer){
            resultLayer.clear();
            for(let i=0;i<dikuaiObjs.length;i++){
                if(dikuaiObjs[i].isInner){
                    var color = analyze.getColor(dikuaiObjs[i].zhibiao, 0, analyze.nowBiaozhun[biaozhun]*0.6, analyze.nowBiaozhun[biaozhun]);
                    var highlightSymbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID,null, 
                        new esri.Color(color));
                    dikuaiObjs[i].g.setSymbol(highlightSymbol);
                    resultLayer.add(dikuaiObjs[i].g);
                    console.log(dikuaiObjs[i].zhibiao)
                }
            }
        }else{
            let geoJSON = {};
            geoJSON.type = 'FeatureCollection';
            geoJSON.features = []
            for(let i=0;i<dikuaiObjs.length;i++){
                if(dikuaiObjs[i].isInner){
                    // debugger
                    let feature = EsriJSONToGeoJSON(dikuaiObjs[i].g);
                    geoJSON.features.push(feature);
                }
            }
            echartsUtils.drawMap(map.extent, resultLayer, JSON.stringify(geoJSON));
        }
    },
    _pg: function(geometry, dikuaiLayer, sheshiLayer, resultLayer, biaozhun, mianji, bufferDistance){
        if(!mapTools){
            mapTools = new MapTools();
        }
        var that = this;
        /** 地块对象 */
        let objs = [];
        queryTask.executeQueryFeatureLayer(dikuaiLayer, geometry, null, null, null, function(innerDikuaiGraphics){
        // queryTask.executeQueryTask(dikuaiLayer, geometry, null, null, null, function(innerDikuaiGraphics){
        // queryTask.executeQueryLocal(dikuaiLayer, geometry, function(innerDikuaiGraphics){
            if(!Array.isArray(innerDikuaiGraphics) && innerDikuaiGraphics.features){
                innerDikuaiGraphics = innerDikuaiGraphics.features;
            }
            // debugger
            for(let i=0;i<innerDikuaiGraphics.length;i++){
                var obj = {
                    g:innerDikuaiGraphics[i],
                    juli:-1,
                    belongto:"",
                    dikuai:innerDikuaiGraphics[i].attributes["FID"],
                    jianzhuliang:0,
                    renkou:innerDikuaiGraphics[i].attributes["人口数量"],
                    yongdidaima: innerDikuaiGraphics[i].attributes["用地代码"],
                    zhibiao:0,
                    renjun:0,
                    isInner:true
                }
                if(!innerDikuaiGraphics[i]){
                    obj.isInner = false;
                }
                objs.push(obj);
            }
            
            that.pgresult(dikuaiLayer.getMap(), geometry, objs, sheshiLayer.graphics,biaozhun || 'lvdi',mianji || this.mapServer.lvdiMianji, bufferDistance || 1000, resultLayer);
            // that.pgresult(geometry, objs, sheshiLayer.graphics,'yiliao',this.mapServer.yiliaoShequMianji, 1000, resultLayer);
        });
        
    },
    pgyiliao: function(geometry, dikuaiLayer, sheshiLayer, resultLayer){
        this._pg(geometry, dikuaiLayer, sheshiLayer, resultLayer, 'yiliao',this.mapServer.yiliaoShequMianji, 1000);
    },
    pgyanglao: function(geometry, dikuaiLayer, sheshiLayer, resultLayer){
        this._pg(geometry, dikuaiLayer, sheshiLayer, resultLayer, 'yanglao',this.mapServer.yiliaoShequMianji, 1000);
    },
    pglvdi: function(geometry, dikuaiLayer, sheshiLayer, resultLayer){
        this._pg(geometry, dikuaiLayer, sheshiLayer, resultLayer, 'lvdi',this.mapServer.lvdiMianji, 1000);
    },
    pg: function(geometry, dikuaiLayer, sheshiLayer, resultLayer, type){
        
        switch (type) {
            case config.analyzeType.YILIAO: 
                this.pgyiliao(geometry, dikuaiLayer, sheshiLayer, resultLayer);
                break;
            case config.analyzeType.YANGLAO:
                this.pgyanglao(geometry, dikuaiLayer, sheshiLayer, resultLayer);
                break;
            case config.analyzeType.LVDI:
                this.pglvdi(geometry, dikuaiLayer, sheshiLayer, resultLayer);
                break;
            default : 
                break;
        }
    },
    /**
	 * 获取渐变颜色 ，小于最小值为白色，大于最大值则为绿色
	 * @param value  实际值
	 * @param min  最小值，为红色
	 * @param mid  中间值，为黄色
	 * @param max  最大值，为绿色
	 * @returns {Array}
	 */
	getColor:function(value,min,mid,max,yongdidaima){
		var minmid = mid-min;
        var midmax = max-mid;
        if(yongdidaima && yongdidaima.indexOf("E1")>-1)
			return [177,229,253];
		if(yongdidaima && yongdidaima=="H21")
			return [130,130,130];
		if(yongdidaima && yongdidaima.indexOf("G")>-1)
			return [115,178,115];
		if(value>=max || value==-1){
			return [0,255,0];
		}else if(value>=mid){
			var midValue = value - mid;
			return [parseInt(255-255*midValue/midmax),255,0];
		}else if(value>=min){
			var minValue = value - min;
			return [255,parseInt(255*minValue/minmid),0];
		}else{
			return [0,0,0];
		}
	},
}

export default analyze